package com.loan.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.loan.common.helper.LoginHelper;
import com.loan.system.domain.ApplyEntity;
import com.loan.system.domain.CollectionLogEntity;
import com.loan.system.mapper.CollectionLogMapper;
import com.loan.system.service.CollectionLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author shiguang on 2022/9/21 15:09
 */
@Service
@RequiredArgsConstructor
public class CollectionLogServiceImpl implements CollectionLogService {

    private final CollectionLogMapper collectionLogMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(ApplyEntity entity, String remark,String detail) {
        CollectionLogEntity collectionLog = new CollectionLogEntity();
        collectionLog.setApplyId(entity.getId());
        collectionLog.setCompanyId(entity.getCompanyId());
        collectionLog.setUserId(entity.getUserId());
        collectionLog.setCollectionUserId(LoginHelper.getUserId());
        collectionLog.setRemark(remark);
        collectionLog.setDetail(detail);
        return collectionLogMapper.insert(collectionLog);
    }

    @Override
    public List<CollectionLogEntity> overLogList(Long userId) {
        return collectionLogMapper.overLogList(userId);
    }

    @Override
    public CollectionLogEntity selectLastLogByApplyId(Long applyId){
       return collectionLogMapper.selectOne(new QueryWrapper<CollectionLogEntity>().eq("apply_id",applyId).orderByDesc("id").last("limit 1"));
    }
}
